package online.sh.cn.fy.users.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * 换购商品 
 */
@WebServlet("/exchangeproduct")
public class ExchangeProductServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		JSONObject json=new JSONObject();
		if(null!=openid && ""!=openid){
			try {
				openid=DES3Util.decrypt(openid);
			} catch (Exception e) {
				log(e.getMessage());
			}
			TokenUtils tu=new TokenUtils();
			String product=request.getParameter("product");
			String extra=request.getParameter("expoints").toString();
			int expoints=1000000000;
			if(extra.equals("a") || extra.equals("b")){
				if(extra.equals("a")){
					expoints=50;
				}else{
					expoints=500;
				}
			}						
			String mobile=tu.getUserMobile(openid);
			if(null!=mobile && ""!=mobile){
				Map map=tu.getFyUserinfoMap(openid);
				int points=(int)map.get("points");
				if(points<expoints){
					json.put("result", 4);		//积分不足导致兑换失败
				}else{
					String result;
					if(expoints==50){
						result=getDanqiping50(mobile);
					}else{
						result=getDanqiping500(mobile);
					}
					if(null!=result && result.equals("0")){
						tu.savePointsEvent(openid, product, "兑换电信宽带氮气加速瓶", "-"+expoints);
						tu.UpdatePointsUse(openid, expoints);
			            json.put("result", 0);
					}else{
						json.put("result", 1);		//兑换失败
					}
				}
			}else{
				json.put("result", 2);			//非认证用户不可兑换
			}
            ResponseUtils.renderJson(response, json.toString());
		}else{
			json.put("result", 3);
			ResponseUtils.renderJson(response, json.toString());
		}
	}
	
	//获取氮气瓶成功与否
	public static String getDanqiping50(String mobile){
		try {
			String url="http://a6.189young.com/feiyoungBottle/feiyoungBottleChannel3Servlet?mobile="+mobile;
			URL getUrl = new URL(url); 
			HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
			connection.connect(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
	        String lines=null; 
	        String s=null;
	        while ((lines = reader.readLine()) != null) { 
	               s=lines; 
	        } 
	        reader.close(); 
	        connection.disconnect(); 
	        return s;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}			

	//获取氮气瓶成功与否
		public static String getDanqiping500(String mobile){
			try {
				String url="http://a6.189young.com/feiyoungBottle/feiyoungBottleChannel2Servlet?mobile="+mobile;
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}			
		
	public static void main(String[] args) {
		System.out.println(getDanqiping500("13672236221"));
	}
}
